package com.atguigu.srb.controller.admin;

import com.atguigu.srb.base.exception.SrbException;
import com.atguigu.srb.common.result.R;
import com.atguigu.srb.common.result.ResultCodeEnum;
import com.atguigu.srb.entity.IntegralGrade;
import com.atguigu.srb.service.IntegralGradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.jnlp.IntegrationService;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.List;

/*
    http+json
        接收请求参数使用json接收
        响应数据使用json格式响应
 */
@RestController //Controller+ResponseBody
@RequestMapping("/admin/integralGrade")
@CrossOrigin //配置允许跨域
public class AdminIntegralGradeController {
    @Autowired
    IntegralGradeService integralGradeService;
    //返回值类型：统一响应类 Result (code message data)
    @GetMapping("list")
    public R<List<IntegralGrade>> list(){
        return R.ok(integralGradeService.list());
    }
    //根据id查询
    @GetMapping("{id}")
    public R<IntegralGrade> getById(@PathVariable("id")Long id){

        return R.ok(integralGradeService.getById(id));
    }
    //删除
    @DeleteMapping("{id}")
    public R<Boolean> deleteById(@PathVariable("id")Long id){

        return R.ok(integralGradeService.removeById(id));
    }
    //更新: 要更新的数据通过请求体提交(提交的对象数据包含id)
    @PutMapping
    public R<Boolean> updateById(@RequestBody IntegralGrade integralGrade){
        if(integralGrade.getId()==null){
//            throw new RuntimeException()
            //业务判断失败，可以失败的原因显示告诉用户
            throw new SrbException(ResultCodeEnum.UPDATE_DATA_ERROR);
        }
        return R.ok(integralGradeService.updateById(integralGrade));
    }
    //新增：  数据没有id
    @PostMapping
    public R<Boolean> save(@RequestBody IntegralGrade integralGrade){

        return R.ok(integralGradeService.save(integralGrade));
    }

}
